package com.hankai.controller.backend;

import com.hankai.common.Const;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.User;
import com.hankai.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by hankai on 2017/6/6.
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManageController {
    @Resource
    private IUserService iUserService;

    /**
     * 管理员登陆
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = iUserService.login(username,password);
        if(!response.isSuccess()){
            return ServerResponse.createByError("输入有误，请重新输入");
        }
        if(!response.getData().getRole().equals(Const.Role.ROLE_ADMIN)){
            return ServerResponse.createByError("你不是管理员");
        }
        session.setAttribute(Const.CURRENT_USER,response.getData());
        return response;

    }
}
